package Practice07;

import java.io.*;
import java.awt.*;
import java.awt.event.*;
public class test2 {
    public static void main(String[] args) {
        new FileFrame();
    }
}
class FileFrame extends Frame implements ActionListener{
    TextArea ta;
    Button open,quit;
    FileDialog fd;
    FileFrame(){
        super("获取并显示文本文件");
        ta=new TextArea(10,45);
        open=new Button("打开");
        quit=new Button("关闭");
        open.addActionListener(this);
        quit.addActionListener(this);
        setLayout(new FlowLayout());
        add(ta);
        add(open);
        add(quit);
        setSize(350,280);
        show();
    }
    public void actionPerformed(ActionEvent e) {
        if(e.getActionCommand()=="打开"){
            fd=new FileDialog(this,"打开文件",FileDialog.LOAD);
            fd.setDirectory("E:\\");//设置文件对话框的基础目录
            fd.show(); //弹出并显示文件对话框，程序暂停直至用户选定一文件
            //改编码格式解决乱码
            System.out.println(fd.getDirectory()+fd.getFile());
            RandomAccessFile raf = null;
            byte[] b = new byte[100];
            try {
                raf = new RandomAccessFile(fd.getDirectory()+fd.getFile(),"rw");
                raf.read(b);
                System.out.println(b);
            } catch (IOException ex) {
                throw new RuntimeException(ex);
            }


        }
        if(e.getActionCommand()=="关闭"){
            dispose();
            System.exit(0);
        }
    }
}
